import { Component, OnInit } from '@angular/core';
import { Router,NavigationExtras } from '@angular/router';
import {HttpClient} from "@angular/common/http";
import {TranslateService} from '@ngx-translate/core';
import { ToastController } from '@ionic/angular';
import { DefaultLanguageService } from '@services/default-language.service';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-signin-first',
  templateUrl: './signin-first.page.html',
  styleUrls: ['./signin-first.page.scss'],
})

export class SigninFirstPage implements OnInit {

  public appUser:any = {
    appUserId:''
  }
  
  public i18n:any = {};

  public appUserIdEmptyMsg:string;

  public loading:boolean = false;

  public validateForm:FormGroup;

  constructor(private router: Router,
              private http:HttpClient,
              private translate: TranslateService,
              private toastController: ToastController,
              private defaultLanguageService:DefaultLanguageService,
              private commonService:CommonService) {
    this.initLanguage();
    this.validateForm = new FormGroup({
      'appUserId':new FormControl('',[
        Validators.required
      ])
    });
  }

  get appUserId(){return this.validateForm.get('appUserId');}

  ngOnInit() {

  }

  initLanguage(){
    let defultLanguage = this.defaultLanguageService.getDefaultLanguage();
    this.translate.setDefaultLang(defultLanguage);
    this.translate.use(defultLanguage);
    this.translate.get(['auth.signFirst','common_safety_verification']).subscribe((jsons)=>{
      for(var item in jsons){
        this.i18n = Object.assign(this.i18n,jsons[item]);
      }
    });
  }

  signIn(){
    this.validateForm.reset();
    this.router.navigate(['/auth/signin-password']);
  }

  async checkUserId(){
    if(this.validateForm.invalid){
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
      return false;
    }
    this.loading = true;
    var api = "/scm/app/checkAppUserExists.do";
    this.http.post(api,this.validateForm.value).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        let isExistsInUser = response.responseBody.isExistsInUser;
        let isExistsInAppUser = response.responseBody.isExistsInAppUser;
        if(isExistsInUser&&!isExistsInAppUser){
          let navigationExtras: NavigationExtras = {
            queryParams: { 'appUserId': this.validateForm.value.appUserId }
          };
          this.router.navigate(['/auth/agreement'],navigationExtras);
          this.validateForm.reset();
        }else if(isExistsInUser&&isExistsInAppUser){
          this.commonService.commonToast(ToastColor.warning,this.i18n.account_exists_tip);
        }else if(!isExistsInUser&&!isExistsInAppUser){
          this.commonService.commonToast(ToastColor.warning,this.i18n.account_not_exists_tip);
        }
      }
      this.loading = false;
    },(errorResponse)=>{
      this.commonService.errorRespCommonToast(errorResponse,this.i18n);
      this.loading = false;
    });
  }

}
